# -*- coding:utf-8 -*-
"""
* User: robertwan
* Date: 11-11-9
* Time: 13锛�20
* Desc:
"""
source_filename = 'ssproto.xml'
utf8head_filename = 'temp_utf8head.xml'
utf8_filename = 'temp_utf8.xml'
order_filename = 'CalProtoMaxSize.cpp'
cppbegin ='#include "CalProtoMaxSize.h"\n\
#include "ssproto.h"\n\n\
int CalProtoMaxSize(NameMap& AllMap,string& MaxName)\n{\n\
    AllMap.insert(make_pair(sizeof(char),"char") );\n'

cppend ='    NameMap::iterator itBegin = AllMap.begin();\n\
    MaxName = itBegin->second;\n\
    return itBegin->first;\n}'

from  xml.dom import  minidom
import codecs
import os
import subprocess;



def get_attrvalue(node, attrname):
     return node.getAttribute(attrname) if node else ''

def get_nodevalue(node, index = 0):
    return node.childNodes[index].nodeValue if node else ''

def get_xmlnode(node,name):
    return node.getElementsByTagName(name) if node else []

def Change_XML_UTF8(filein,fileout):
    final = codecs.open(filein, 'rb', 'mbcs')
    text = final.read().encode('utf-8')
##    print(text)
    final.close
    final = open(fileout, 'wb')
    final.write(text)
##    print(text)
    final.close
    print("Change_XML_UTF8 Success")

#鏀瑰彉浜唜ml鐨別ncode鏍囪瘑
def Change_XML_Head(filein,fileout):
    infile = open(filein,'r')
    outfile = open(fileout,'w')
    for line in infile:
        line = line.replace('encoding="GBK"','encoding="utf-8"')
        outfile.write(line)
##        print(line)
    infile.close
    outfile.close
    print ("Change_XML_Head success")

#鑾峰彇xml涓殑struct鎴愬憳
def get_xml_data(filename):
##    print(filename)
    doc = minidom.parse(filename)
    root = doc.documentElement
    user_nodes = get_xmlnode(root,'struct')
##    print (user_nodes)
    user_list=[]
    for node in user_nodes:
        user_id = get_attrvalue(node,'name')
        user = {}
        user['name'] = user_id
        user_list.append(user)
    return user_list

#鍒涘缓浠ｇ爜
def Create_Cpp(StructList):
    f = open(order_filename,"w")
    f.write(cppbegin);
    for Struct in StructList:
        f.write(Struct)            
    f.write(cppend);
    f.close();
    
#鎷煎噾鍑虹敓鎴愪唬鐮�  
def Change_XML_Tocpp(filename):
    AddList= []
    user_list = get_xml_data(filename)
    i = 0
    for user in user_list :
        if user:
            i = i+1
            user_str='    AllMap.insert(make_pair(sizeof(tag%s),"%s") );\n' % (user['name'].strip(),user['name'])
            AddList.append(user_str)
##            print (user_str)
    Create_Cpp(AddList)
    print ("Change_XML_Tocpp Success")
    

#创建h  
def genTDR( exe_fn, in_fn ):
    #"%TOOLS_HOME%/tdr.exe" -H -O %CS_PROTOCOL_H% %CS_XML_FILES%
    exe_fn = "tdr.exe"
    print(exe_fn)
    buildstr = exe_fn + " -H -o " + "ssproto.h ssproto.xml "
    print(buildstr)
    nResult = os.system(buildstr)
    if nResult == 0:
        print('tdr ssproto.h seccuss %d'%nResult)
    else:
        print('tdr ssproto.h fail %d'%nResult)
    
##    print(str(subprocess.Popen( [exe_fn, "-H", "-O", "ssproto.h", in_fn ], stdout=subprocess.PIPE ).communicate()[0], "mbcs" ) );
    


  

if __name__ == "__main__":
    Change_XML_Head(source_filename,utf8head_filename)
    Change_XML_UTF8(utf8head_filename,utf8_filename)
    Change_XML_Tocpp(utf8_filename)
    genTDR("ssproto.h","ssproto.xml")
